<template>
  <div class="register-page">
    <div class="register-container">
      <div class="register-image-container">
        <img src="../assets/222.png" alt="注册图片" width="380px" height="380px" />
      </div>
      <div class="register-card">
        <div class="zhuce">
          <p>用户注册</p>
          <div @click="$router.push('/login')" class="zhanghao">
            <span class="no-account">已有账号</span>，
            <span class="click-register">直接登录</span>
          </div>
        </div>
        <el-form :model="registerForm" ref="registerForm" :rules="rules" label-width="0">
          <el-form-item prop="phone_num">
            <el-input v-model="registerForm.phone_num" placeholder="手机号/邮箱" prefix-icon="el-icon-message"></el-input>
          </el-form-item>

          <div class="users">
            <el-form-item prop="username">
              <el-input v-model="registerForm.username" placeholder="用户名" prefix-icon="el-icon-message"></el-input>
            </el-form-item>
            <div class="role">
              <label>身份：</label>
              <select v-model="registerForm.role">
                <option value="student">学生</option>
                <option value="teacher">老师</option>

              </select>
            </div>
           
          </div>
          <div prop="capcha" v-if="registerForm.role === 'student'" class="verification-item">
              <input type="text" v-model="registerForm.stu_number" class="input-width" placeholder="学号" />

            </div>
          <el-form-item prop="password">
            <el-input type="password" v-model="registerForm.password" placeholder="设置密码"
              prefix-icon="el-icon-lock"></el-input>
          </el-form-item>
          <el-form-item prop="confirmPassword">
            <el-input type="password" v-model="registerForm.confirmPassword" placeholder="确认密码"
              prefix-icon="el-icon-lock"></el-input>
          </el-form-item>
          <div class="shouce">
            <el-checkbox v-model="registerForm.agree">
              <span class="no-account1">同意并愿意遵守</span>，
              <span class="click-register1">《****服务协议》</span>
            </el-checkbox>
          </div>
          <el class="anniu">
            <el-button type="primary" @click="handleRegister" round class="anniu">立即注册</el-button>
          </el>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
// import axios from "../api/axios";
import api from '../api';

export default {
  name: "StudentRegister",
  data() {
    return {
      registerForm: {
        username: "",
        phone_num: "",
        capcha: "",
        stu_number: '',
        password: "",
        confirmPassword: "",
        role: "",
        agree: false,
      },
      registeredUsers: [], // 新增一个数组来存储已注册的用户名
      rules: {
        phone_num: [
          { required: true, message: "请输入邮箱或手机号", trigger: "blur" },
        ],
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        stu_number: [
          { required: true, message: "请输入学号", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        confirmPassword: [
          { required: true, message: "请确认密码", trigger: "blur" },
        ],
        role: [{ required: true, message: "请选择身份", trigger: "change" }],
      },
    };
  },
  methods: {
    handleRegister() {
      // 校验密码和确认密码是否一致
      if (this.registerForm.password !== this.registerForm.confirmPassword) {
        this.$message.error("密码不一致，请修改");
        return;
      }

      // 检查是否勾选同意协议
      if (!this.registerForm.agree) {
        this.$message.error("请勾选同意使用该账号");
        return;
      }

      // 使用 this.registerForm 中的字段构建请求参数对象
      const requestData = {
        phone_num: this.registerForm.phone_num,
        username: this.registerForm.username,
        stu_number: this.registerForm.stu_number,
        password: this.registerForm.password,
        user_type: this.registerForm.role,
      };

      // 发起注册请求
      api.Register(requestData)
        .then((response) => {
          if (response.data.status) {

            this.$message.success("注册成功,请登录");
            localStorage.setItem("auth", true);
            localStorage.setItem("teacherName", this.registerForm.phone_num);
            localStorage.setItem("stu_number", this.registerForm.stu_number);



            this.$router.push({
              path: '/login',

            });
          } else {
            this.$message.error("注册失败，请重试");
          }
        })
        .catch((error) => {
          console.error(error);
          this.$message.error("注册失败，请稍后再试");
        });
    },
  },
};
</script>

<!-- <style src="../css/register.css"></style> -->
<style scoped>
p {
  font-size: 24px;
  margin-top: 10px;
  height: 30px;
  color: rgba(0, 0, 0, 1);
}

h2 {
  text-align: center;
  margin-bottom: 20px;
  color: #409eff;
}

.register-container {
  animation: swing 1.5s ease-in-out infinite, swing-glow 1.5s ease-in-out infinite;
}

.register-container:hover {
  animation-play-state: paused;
}

.register-page {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #409EFF;
}

.register-container {
  display: flex;
  width: 800px;
  height: 400px;
  background-color: #fff;
  border-radius: 30px;
  overflow: hidden;
}

.zhuce {
  display: flex;
  flex-direction: row;
  align-items: right;
  width: 300px;
  height: 50px;

}

.anniu {
  border-radius: 14px;
  width: 250px;
  margin-left: 5px;

}

.role {
  display: flex;
  flex-direction: row;
  width: 50%;
  height: 100%;
  margin-left: 10%;
  margin-top: 3px;
}

.users {
  display: flex;
  flex-direction: row;
}

.zhanghao {
  display: flex;
  margin-top: 15px;
  margin-left: 70px;
  height: 30px;
  cursor: pointer;
}

.no-account {
  color: rgba(57, 61, 66, 1);
  font-size: 14px;
  align-items: right;
  height: 30px;
}

.click-register {
  height: 30px;
  color: #409EFF;
  /* 点击注册显示为蓝色 */
  font-size: 14px;
  cursor: pointer;
}

.register-image-container {
  flex: 2;
  background-color: #fff8f2;
  display: flex;
  align-items: center;
  justify-content: center;
  border-top-left-radius: 30px;
  border-bottom-left-radius: 30px;
}

.register-card {
  flex: 1;
  padding: 40px;
  background: white;
  border-radius: 12px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.el-input,
.el-select {
  width: 100%;
  margin-top: -5px;
}

.verification-item {
  display: flex;
  margin-top: -5px;

}

.input-width {
  width: 300px;
  background: rgba(255, 255, 255, 1);
  height: 32px;
  border: 1px solid #dcdfe6;
  color: #666;
  /* 设置输入文字颜色为灰色 */
  border-radius: 4px;
  padding-left: 30px;
  margin-bottom: 13px;
}

.input-width:focus {
  outline: none;
  /* 去掉输入框获取焦点时的默认边框 */
}

.button {
  border-radius: 14px;
}

.verification-item .el-input {
  flex: 1;
}

.verification-item .el-button {
  margin: 2px 0px 18px 20px;
}

.el-button {
  width: 100%;
  background-color: #409EFF;
  border: none;
  margin-top: 20px;
  margin-left: 0px;
  padding-right: 10px;

}

.el-link {
  display: block;
  text-align: center;
  margin-top: 10px;
}

.el-checkbox {
  margin-top: -10px;
  margin-bottom: 5px;
  height: 25px;

}

.shouce {
  margin-left: 00px;
  height: 0px;
  margin-top: 0px;
  display: flex;
}


.no-account1 {
  color: rgba(57, 61, 66, 1);
  font-size: 12px;
  align-items: right;
  margin-top: 0px;
}

.click-register1 {
  height: 30px;
  color: #409EFF;
  /* 点击注册显示为蓝色 */
  font-size: 12px;
}
</style>
